<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('证书域名管理列表')" />
    <th:block th:include="include :: layout-latest-css" />
    <th:block th:include="include :: ztree-css" />
</head>
<body class="gray-bg">
<!--<div class="ui-layout-west">-->
    <!--    <div class="box box-main">
            <div class="box-header">
                <div class="box-title">
                    <i class="fa fa-sitemap"></i> 托管服务器
                </div>
                <div class="box-tools pull-right">
                    <a type="button" class="btn btn-box-tool" href="javascript:void(0)" onclick="serviceToPage()" title="托管服务器管理"><i class="fa fa-edit"></i></a>
                    <button type="button" class="btn btn-box-tool" id="btnRefresh" title="刷新服务器"><i class="fa fa-refresh"></i></button>
                </div>
            </div>
            <div class="ui-layout-content">
                <div id="tree" class="ztree"></div>
            </div>
        </div>
    </div>
    <div class="ui-layout-center">-->
    <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <input type="hidden" name="stsId" id="stsId"/>
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>证书类型：</label>
                                <select name="isUniversal">
                                    <option value="">所有</option>
                                    <option value="Y">泛域名</option>
                                    <option value="N">普通域名</option>
                                </select>
                            </li>
                            <li>
                                <label>颁发机构：</label>
                                <select name="issuingAuthority" th:with="type=${@dict.getType('lssuing_authority')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <label>证书域名：</label>
                                <input type="text" name="certificateDomain"/>
                            </li>
                            <li>
                                <label>状态：</label>
                                <select name="status" th:with="type=${@dict.getType('ssl_status')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="openApplySSL()" shiro:hasPermission="SSL:domain:add">
                    <i class="fa fa-plus"></i> 申请证书
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="SSL:domain:remove">
                    <i class="fa fa-remove"></i> 删除证书
                </a>
            </div>
            <div class="col-sm-12 select-table table-bordered">
                <table id="bootstrap-table"></table>
            </div>
<!--        </div>-->
    </div>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: layout-latest-js" />
<th:block th:include="include :: ztree-js" />
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('SSL:domain:edit')}]];
    var removeFlag = [[${@permission.hasPermi('SSL:domain:remove')}]];
    var issuingAuthorityDatas = [[${@dict.getType('lssuing_authority')}]];
    var statusDatas = [[${@dict.getType('ssl_status')}]];
    var prefix = ctx + "SSL/domain1";

    $(function() {
        /*var panehHidden = false;
        if ($(this).width() < 769) {
            panehHidden = true;
        }
        $('body').layout({ initClosed: panehHidden, west__size: 270, resizeWithWindow: false });
        // 回到顶部绑定
        if ($.fn.toTop !== undefined) {
            var opt = {
                win:$('.ui-layout-center'),
                doc:$('.ui-layout-center')
            };
            $('#scroll-up').toTop(opt);
        }*/
        queryDataList();
        // queryServiceTree();
    });

    /*function serviceToPage() {
        var url = ctx + "SSL/service";
        $.modal.openTab("托管服务器管理", url);
    }*/

    function openApplySSL(){
        $.modal.openTab('申请证书', prefix+"/add");
    }

    $('#btnRefresh').click(function() {
        queryServiceTree();
    });

    function queryServiceTree(){
        var url = ctx + "SSL/domain/queryServiceTree";
        var options = {
            url: url,
            expandLevel: 2,
            onClick : zOnClick
        };
        $.tree.init(options);
        function zOnClick(event, treeId, treeNode) {
            $("#stsId").val(treeNode.id);
            $.table.search();
        }
    }

    function queryDataList() {
        var serId = [[${serId}]]
        if(serId){
            $("#stsId").val(serId);
        }
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "证书管理",
            columns: [{
                checkbox: true
            },
                {
                    field: 'id',
                    title: 'Id',
                    visible: false
                },
                {
                    field: 'createTime',
                    title: '创建时间'
                },
                {
                    field: 'issuingAuthority',
                    title: '颁发机构',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(issuingAuthorityDatas, value);
                    }
                },
                {
                    field: 'certificateDomain',
                    title: '证书域名'
                },
                {
                    field: 'isUniversal',
                    title: '证书类型',
                    formatter: function(value, row, index) {
                        if(value === 'Y'){
                            return '<p><span class="badge badge-primary">泛域名</span></p>';
                        }else{
                            return '<p><span class="badge badge-info">普通域名</span></p>';
                        }
                    }
                },
                {
                    field: 'daysDifference',
                    title: '证书过期时间',
                    formatter:function (value, row, index){
                        if(value){
                            if(value > 30){
                                return '<p><span class="badge badge-success">剩余：'+value+' 天</span></p>';
                            }else if (value <=30 && value > 20){
                                return '<p><span class="badge badge-primary">剩余：'+value+' 天</span></p>';
                            }else if (value <=20 && value > 10){
                                return '<p><span class="badge badge-warning">剩余：'+value+' 天</span></p>';
                            }else{
                                return '<p><span class="badge badge-danger">剩余：'+value+' 天</span></p>';
                            }
                        }else{
                            return '<p><span class="badge badge-danger">剩余：0 天</span></p>';
                        }
                    }
                },
                {
                    field: 'isAutoSsl',
                    title: '自动部署、续签',
                    formatter: function(value, row, index) {
                        return isAutoSslTools(row);
                    }
                },
                {
                    field: 'autoDaysDifference',
                    title: '续签过期时间',
                    formatter:function (value, row, index){
                        var tmpHtml="";
                        if(value){
                            if(value > 30){
                                tmpHtml= '<span class="badge badge-success">剩余：'+value+' 天</span>';
                            }else if (value <=30 && value > 20){
                                tmpHtml= '<span class="badge badge-primary">剩余：'+value+' 天</span>';
                            }else if (value <=20 && value > 10){
                                tmpHtml= '<span class="badge badge-warning">剩余：'+value+' 天</span>';
                            }else if(value > 0 && value<=10){
                                tmpHtml= '<span class="badge badge-danger">剩余：'+value+' 天</span>';
                            }else{
                                tmpHtml= '<span class="badge badge-danger">已过期</span>';
                            }
                        }else{
                            tmpHtml= '<span class="badge badge-danger">已过期</span>';
                        }
                        return '<p>'+tmpHtml+'<span style="margin-left: 5px;cursor: pointer;" onclick="toSetTimePage(\'' + row.id + '\')" class="badge badge-info">设置</span></p>';
                    }
                },
                {
                    field: 'status',
                    title: '状态',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(statusDatas, value);
                    }
                },
                {
                    field: 'deployStatus',
                    title: '部署状态',
                    formatter: function(value, row, index) {
                        if(value === 'Y'){
                            return "<span class='badge badge-primary'>已部署成功</span>";
                        }else if(value === 'N' || !value){
                            return "<span class='badge badge-danger'>未部署</span>";
                        }
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        if((row.status === '3' || row.status === '4') && row.sslApplyId) {
                            actions.push('<a class="btn btn-success btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="viewSSLContent(\'' + row.sslApplyId + '\')"><i class="fa fa-eye"></i>查看证书</a> ');
                        }else if(row.status === '3' || row.status === '4' && !row.sslApplyId){
                            actions.push('<a class="btn btn-success btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="viewSSLContent(\'' + row.sslApplyId + '\')"><i class="fa fa-eye"></i>未同步到证书内容</a> ');
                        }else if(row.status === '5' || row.status === '-1'){
                            actions.push('<a class="btn btn-success btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="retryApply(\'' + row.id + '\')"><i class="fa fa-paper-plane"></i>重新申请</a> ');
                        }
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    }


    function authorizations(id){
        $.modal.customOpen('验证域名所有权', ctx + "SSL/apply/authorizations/"+id, "开始验证", "取消", $(window).width()*0.8);
    }

    function viewSSLContent(id){
        $.modal.openTab('查看下载证书', ctx + "SSL/apply/downFilePage/"+id);
    }

    function toSetTimePage(id){
        $.modal.openTab('设置续签过期时间', prefix + "/setTime/"+id);
    }

    function retryApply(id){
        $.modal.confirm("确认要重新申请吗？（注：没有签发成功不扣减数量）", function() {
            $.operate.get(prefix+"/retryApply/"+id)
        })
    }

    function isAutoSslTools(row) {
        if (row.isAutoSsl === 'N' || !row.isAutoSsl) {
            return '<i class=\"fa fa-toggle-off text-info fa-2x\" onclick="enable(\'' + row.id + '\')"></i> ';
        } else if(row.isAutoSsl === 'Y'){
            return '<i class=\"fa fa-toggle-on text-info fa-2x\" onclick="disable(\'' + row.id + '\')"></i> ';
        }
    }

    function disable(id) {
        $.modal.confirm("确认要关闭自动部署、续签吗？", function() {
            $.operate.post(prefix + "/edit", { "id": id, "isAutoSsl": 'N' });
        })
    }

    function enable(id) {
        $.modal.confirm("确认要开启自动部署、续签吗？", function() {
            $.ajax({
                url: prefix + "/isYz/"+ id,
                type: 'get',
                success:function (res){
                    if(res.code !== 0){
                        $.modal.confirm(res.msg, function() {
                            $.modal.openTab("自动部署、续签设置", prefix+"/sedInfo/"+id);
                        })
                    }else if(res.code === 0){
                        $.table.search();
                    }
                },
                field:function (error){}
            })
        })
    }
</script>
</body>
</html>
